from manim import *
class math(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text = Tex(r"\text{设P为曲线}$\left\{\begin{matrix}x=\cos t \\y=2\sin ^{2}t\end{matrix}\right.,t\in \left[ 0,\dfrac{\pi }{2}\right] $"
                   r"\text{上的一点,该曲线与直线OP及x轴}"
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,4.5,0])
        text1 = Tex(r"\text{所围图形的面积为S,求函数}$\dfrac{dS}{dt}$\text{取得最大值时点P的坐标}"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-8,-2.5,0])
        self.play(Write(text),Write(text1),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text.animate().move_to([0,22,0]) ,text1.animate().move_to([-8,15,0]))
        
        #下划线
        dot1=Dot([-40,19,0])
        dot2=Dot([10,19,0])
        line=Line(dot1,dot2).set_color(YELLOW)
        line.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line))

        #下划线
        dot3=Dot([12.5,19,0])
        dot4=Dot([42,19,0])
        line1=Line(dot3,dot4).set_color(YELLOW)
        line1.set_stroke(width=30)  # 设置宽度为 30
         #下划线
        dot5=Dot([-40,13,0])
        dot6=Dot([-18,13,0])
        line2=Line(dot5,dot6).set_color(YELLOW)
        line2.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line1),Create(line2),FadeOut(line))

        rectangle=Rectangle(color=BLUE,fill_opacity=0.3,width=41,height=6
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([5, 15, 0])
        self.play(Create(rectangle),FadeOut(line1),FadeOut(line2))
        self.play(
            rectangle.animate.move_to([-6,15,0]).stretch_to_fit_width(4)
        )
        self.play(
            rectangle.animate.move_to([-18.5,15,0]).stretch_to_fit_width(3)
        )
        self.play(
            rectangle.animate.move_to([-36,15,0]).stretch_to_fit_width(10)
        )
        rectangle1=Rectangle(color=GREEN,fill_opacity=0.3,width=20,height=6
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle1.move_to([-22.5, 22, 0])
        self.play(Create(rectangle1),FadeOut(rectangle))
        text2 = Tex(r"$y=2\left( 1-\cos ^{2}t\right) $"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,5,0])
        self.play(ReplacementTransform(rectangle1,text2))
        self.wait(0.2)
        text3 = Tex(r"\text{曲线:}$y=2\left( 1-x^{2}\right) $",color=YELLOW
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,5,0])
        self.play(ReplacementTransform(text2,text3))
        self.wait(0.2)
        rectangle2=Rectangle(color=RED,fill_opacity=0.3,width=12,height=6
                            ,stroke_color=RED,stroke_width=20)
        rectangle2.move_to([-6, 22, 0])
        self.play(Create(rectangle2))
        text3 = Tex(r"$0\leq x\leq 1$"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,5,0])
        self.play(ReplacementTransform(rectangle2,text3))
        self.wait(0.2)

        text4 = Tex(r"$S=\dfrac{1}{2}\cdot x\cdot y+\int ^{1}_{x}2\left( 1-t^{2}\right) dt$"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-20,0])
        self.play(Write(text4))
        text5 = Tex(r"$=-\dfrac{x^{3}}{3}-x+\dfrac{4}{3}$"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text4,RIGHT,buff=1)
        self.play(Write(text5))
        rectangle3=Rectangle(color=GREEN,fill_opacity=0.3,width=15,height=6
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle3.move_to([27.5, -20, 0])
        self.play(Create(rectangle3))
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.3,width=4,height=6
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle4.move_to([-6, 15, 0])
        self.play(Create(rectangle4))
        text6 = Tex(r"$\dfrac{dS}{dx}\cdot \dfrac{dx}{dt}$"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([10,5,0])
        self.play(ReplacementTransform(rectangle4,text6))
        self.wait(0.2)
        text7 = Tex(r"$=\left( -x^{2}-1\right) $"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,RIGHT,buff=1)
        self.play(Write(text7))
        text8 = Tex(r"$ \left( -\sin t\right) $"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text7,RIGHT,buff=0.5)
        self.play(Write(text8))
        text70 = Tex(r"$=\left( x^{2}+1\right) $"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text6,RIGHT,buff=1)

        text80 = Tex(r"$ \left( \sin t\right) $"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text70,RIGHT,buff=0.5)




        self.play(ReplacementTransform(text7,text70),ReplacementTransform(text8,text80))
        self.wait(0.2)
        text800 = Tex(r"$ \left( \sqrt{\dfrac{y}{2}}\right) $"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text70,RIGHT,buff=0.5)
        self.play(ReplacementTransform(text80,text800))
        self.wait(0.2)
        text8000 = Tex(r"$ \left(\sqrt{1-x^{2}}\right) $"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text70,RIGHT,buff=0.5)
        self.play(ReplacementTransform(text800,text8000))
        self.wait(0.2)

         #下划线
        dot7=Dot([19,2,0])
        dot8=Dot([37,2,0])
        line3=Line(dot7,dot8).set_color(YELLOW)
        line3.set_stroke(width=30)  # 设置宽度为 30
        text9 = Tex(r"$f\left( x\right) $"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(line3,DOWN,buff=1)
       
        self.play(Create(line3),Write(text9))
        text10 = Tex(r"\text{设}$f\left( x\right)=\left( x^{2}+1\right) \cdot \sqrt{1-x^{2}} ,0\leq x\leq 1$"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-5,0])
        self.play(ReplacementTransform(text3,text10))

        self.wait(0.2)

        rectangle5=Rectangle(color=BLUE,fill_opacity=0.3,width=46,height=6
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle5.move_to([20,-5,0])
        self.play(Create(rectangle5))
        text11 = Tex(r"$f'\left( x\right) =2x\sqrt{1-x^{2}}+\left( x^{2}+1\right) \cdot \dfrac{-x}{\sqrt{1-x^{2}}}$"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-10,0])
        self.play(ReplacementTransform(rectangle5,text11))
        text12 = Tex(r"$f'\left( x\right) =\dfrac{x}{\sqrt{1-x^{2}}}\cdot \left[ 1-3x^2\right] $"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([20,-12,0])
        self.play(ReplacementTransform(text11,text12),FadeOut(text4,text5,rectangle3))

        rectangle6=Rectangle(color=BLUE,fill_opacity=0.3,width=10,height=6
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle6.move_to([19,-12,0])
        self.play(Create(rectangle6))

        rectangle7=Rectangle(color=GREEN,fill_opacity=0.3,width=9,height=6
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle7.move_to([31,-12,0])
        self.play(Create(rectangle7))
        text13 = Tex(r"\text{当}$0\leq x\leq \dfrac{\sqrt{3}}{3}$\text{时,}"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-20,0])
        self.play(Write(text13))
        text14 = Tex(r"\text{有}$f'\left( x\right) > 0,$"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text13,RIGHT,buff=1)
        self.play(Write(text14))
        text15 = Tex(r"\text{则}$f\left( x\right) $\text{单调递增}"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=1)
        self.play(Write(text15))


        text13 = Tex(r"\text{当}$\dfrac{\sqrt{3}}{3} <x\leq 1$\text{时,}"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-26,0])
        self.play(Write(text13))
        text14 = Tex(r"\text{有}$f'\left( x\right) < 0,$"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text13,RIGHT,buff=1)
        self.play(Write(text14))
        text15 = Tex(r"\text{则}$f\left( x\right) $\text{单调递减}"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text14,RIGHT,buff=1)
        self.play(Write(text15))
        text16 = Tex(r"$x=\dfrac{\sqrt{3}}{3}$\text{时}$y=\dfrac{4}{3}$"
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-3,0])
        self.play(Write(text16))
        text17 = Tex(r"$P\left( \dfrac{\sqrt{3}}{3},\dfrac{4}{3}\right) $",color=RED
                   
                   
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,-3,0])
        self.play(ReplacementTransform(text16,text17))

        self.wait()




